package com.ruoyi.eqmonitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.eqmonitor.dispose.service.IExteriorSiteAlarmService;
import com.ruoyi.eqmonitor.dtos.AlarmSearchInput;
import com.ruoyi.eqmonitor.vo.CockpitInfoVo;
import com.ruoyi.eqmonitor.dtos.CockpitRiverwayAlarmDto;
import com.ruoyi.eqmonitor.dtos.OptDeviceAlarmDto;
import com.ruoyi.eqmonitor.mapper.OptPatrolEventMapper;
import com.ruoyi.eqmonitor.service.CockpitService;
import com.ruoyi.eqmonitor.service.IOptDeviceAlarmService;
import com.ruoyi.eqmonitor.service.IOptDeviceService;
import com.ruoyi.eqmonitor.smdtv.domain.SmdtvSewagePlant;
import com.ruoyi.eqmonitor.smdtv.service.*;
import com.ruoyi.eqmonitor.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

@Slf4j
@Service
public class CockpitServiceImpl implements CockpitService {

    @Resource
    private ISmdtvOutletProblemService smdtvOutletProblemService;
    @Resource
    private IExteriorSiteAlarmService exteriorSiteAlarmService;
    @Resource
    private ISmdtvOverflowWellService smdtvOverflowWellService;
    @Resource
    private IOptDeviceAlarmService optDeviceAlarmService;
    @Resource
    private OptPatrolEventMapper optPatrolEventMapper;
    @Resource
    private IOptDeviceService optDeviceService;
    @Resource
    private SmregisterService smregisterService;
    @Resource
    private ISmdtvSewagePlantService iSmdtvSewagePlantService;
    @Resource
    private ISmdtvFractureSurfaceService iSmdtvFractureSurfaceService;

    @Override
    public List<OptDeviceAlarmDto> cockpitRiverwayAlarm(CockpitRiverwayAlarmDto dto) throws ParseException {
        Integer alarmType = dto.getAlarmType();
        Integer alarmCode = dto.getAlarmCode();
        switch (alarmType) {
            case 1: //排口
                return smdtvOutletProblemService.cockpitRiverwayAlarm(dto);
            case 2: //溢流井
                return smdtvOverflowWellService.cockpitRiverwayAlarm(dto);
            case 3: //断面
                return exteriorSiteAlarmService.cockpitRiverwayAlarm(dto);
            case 4: //一杆式河道监测站
                AlarmSearchInput alarmDto = new AlarmSearchInput();
                alarmDto.setAreaName(dto.getAreaName());
                alarmDto.setDeviceType(Constant.EquipmentType.SE200_L_Y0.getCode());
                alarmDto.setAlarmPhenomenon(11);
                if (alarmCode == 1) {
                    alarmDto.setPersistentStatus(0);
                } else if (alarmCode == 2) {
                    alarmDto.setPersistentStatus(1);
                }
                return optDeviceAlarmService.listAndDispatch(alarmDto);
            /*case 5: //氨氮一体机-流量
                AlarmSearchInput alarmDto2 = new AlarmSearchInput();
                alarmDto2.setAreaName(dto.getAreaName());
                alarmDto2.setDeviceType(Constant.EquipmentType.SR500_L_Y0_LDN.getCode());
                alarmDto2.setAlarmPhenomenon(12);
                if (alarmCode == 1) {
                    alarmDto2.setPersistentStatus(0);
                } else if (alarmCode == 2) {
                    alarmDto2.setPersistentStatus(1);
                }
                return optDeviceAlarmService.listAndDispatch(alarmDto2);
            case 6: //氨氮一体机-氨氮
                AlarmSearchInput alarmDto3 = new AlarmSearchInput();
                alarmDto3.setAreaName(dto.getAreaName());
                alarmDto3.setDeviceType(Constant.EquipmentType.SR500_L_Y0_LDN.getCode());
                alarmDto3.setAlarmPhenomenon(6);
                if (alarmCode == 1) {
                    alarmDto3.setPersistentStatus(0);
                } else if (alarmCode == 2) {
                    alarmDto3.setPersistentStatus(1);
                }
                return optDeviceAlarmService.listAndDispatch(alarmDto3);*/
            default:
                return new ArrayList<OptDeviceAlarmDto>();
        }
    }

    @Override
    public CockpitInfoVo getInfo(Long id, Integer alarmType) {
        String minorId = null;
        if (alarmType == 1 || alarmType == 2 || alarmType == 3) {
            if (alarmType == 1) {
                minorId = "排口" + id;
            } else if (alarmType == 2) {
                minorId = "溢流井" + id;
            } else {
                minorId = "断面" + id;
            }
        }
        return optPatrolEventMapper.getInfo(minorId, id);
    }

    @Override
    public Map<String, String> selectCount(String name) {
        Map<String, String> map = new HashMap<>();
        //设备数量
        List<Map<String, String>> device = optDeviceService.mySelectCount(name);
        device.forEach(d -> {
            map.put(d.get("deviceType"), String.valueOf(d.get("count")));
        });
        //构建查询条件（超图各表中的街道字段不统一）
        String smdtv2Where = "";
        String smdtv11Where = "";
        String smdtv8Where = "";
        String smdtv10Where = "";
        String smdtv18Where = "";
        String smdtv14Where = "";
        String smdtv19Where = "";
        String smdtv9Where = "";
        String smdtv1Where = "";
        String smdtv6Where = "";
        String smdtv5Where = "";
        if (name != null && !"".equals(name)) {
            smdtv2Where = " where TOWN like '%" + name + "%'";
            smdtv11Where = " where TOWN = '" + name + "'";
            smdtv8Where = " where TOWN = '" + name + "'";
            smdtv10Where = " where street = '" + name + "'";
            smdtv18Where = " where TOWN = '" + name + "'";
            smdtv14Where = " where TOWN = '" + name + "'";
            smdtv19Where = " where TOWN = '" + name + "'";
            smdtv9Where = " where PJX = '" + name + "'";
            smdtv1Where = " where TOWN = '" + name + "'";
            smdtv6Where = " where TOWN = '" + name + "'";
            smdtv5Where = " where TOWN = '" + name + "'";
        }
        Integer smdtv2 = smregisterService.selectCount(Constant.Smregister.SMDTV_2.getName(), smdtv2Where);
        map.put("河道", smdtv2.toString());
        Integer smdtv11 = smregisterService.selectCount(Constant.Smregister.SMDTV_11.getName(), smdtv11Where);
        map.put("闸口", smdtv11.toString());
        Integer smdtv8 = smregisterService.selectCount(Constant.Smregister.SMDTV_8.getName(), smdtv8Where);
        map.put("排口", smdtv8.toString());
        Integer smdtv10 = smregisterService.selectCount(Constant.Smregister.SMDTV_10.getName(), smdtv10Where);
        map.put("溢流井", smdtv10.toString());
        Integer smdtv18 = smregisterService.selectCount(Constant.Smregister.SMDTV_18.getName(), smdtv18Where);
        map.put("排水户", smdtv18.toString());
        Integer smdtv14 = smregisterService.selectCount(Constant.Smregister.SMDTV_14.getName(), smdtv14Where);
        map.put("污水泵站", smdtv14.toString());
        Integer smdtv19 = smregisterService.selectCount(Constant.Smregister.SMDTV_19.getName(), smdtv19Where);
        map.put("农村水终端", smdtv19.toString());
        //断面(微站)
        Integer smdtv9 = smregisterService.selectCount(Constant.Smregister.SMDTV_9.getName(), smdtv9Where);
        map.put("断面", smdtv9.toString());
        Integer smdtv1 = smregisterService.selectCount(Constant.Smregister.SMDTV_1.getName(), smdtv1Where);
        map.put("池塘", smdtv1.toString());
        Integer smdtv6 = smregisterService.selectCount(Constant.Smregister.SMDTV_6.getName(), smdtv6Where);
        map.put("水库", smdtv6.toString());
        Integer smdtv5 = smregisterService.selectCount(Constant.Smregister.SMDTV_5.getName(), smdtv5Where);
        map.put("山塘", smdtv5.toString());
        if (name == null || "".equals(name)) {
            long dm = iSmdtvFractureSurfaceService.count();
            map.put("县控以上断面", String.valueOf(dm));
        } else {
            map.put("县控以上断面", "0");
        }
        LambdaQueryWrapper<SmdtvSewagePlant> lqw = new LambdaQueryWrapper<>();
        if (name != null && !"".equals(name)) {
            lqw.eq(SmdtvSewagePlant::getSsjd, name);
        }
        long wsc = iSmdtvSewagePlantService.count(lqw);
        map.put("污水厂", String.valueOf(wsc));
        return map;
    }

}
